from cmath import log
import math
from pstats import Stats
import numpy as np
import pandas as pd
import scipy.optimize as optimize
import scipy.interpolate as spi
import random
import matplotlib.pyplot as plt
from astropy.modeling import models, fitting
import scipy.stats as stats
from scipy import interpolate
import statsmodels.api as sm
import warnings

#---------------------------------------定义故障分布函数

'''
利用三次样条插值法计算故障率
'''
def func_cdf_cub(x, a,b,c):   
    t = (a,b,c) 
    return interpolate.splev(x,t)

'''
计算高斯分布时的故障率
'''
def func_cdf_gau(x, miu, sigma):    
    warnings.filterwarnings('error')
    try:
        return 1-stats.norm.cdf((np.array(x)-miu)/sigma)
    except:
        return '输入参数异常'

'''
计算对数高斯分布时的故障率
'''
def func_cdf_loggau(x, miu, sigma):    
    warnings.filterwarnings('error')
    try:
        return 1-stats.norm.cdf((np.log(np.array(x))-miu)/sigma)
    except:
        return '输入参数异常'    

'''
计算威布尔分布时的故障率
'''
def func_weib(x, m, gamma, t0):
    warnings.filterwarnings('error')
    try:
        x = np.array(x)
        return np.exp(-1 * (x - gamma) ** m / t0)
    except: 
        return '输入参数异常' 

'''
计算指数分布时的故障率
'''
def func_expon(x, b):
    warnings.filterwarnings('error')
    try:
        return np.exp(-b / np.array(x))
    except: 
        return '输入参数异常' 


#————————————————————————————
#指数分布求剩余工作时间
def find_expon(reliability, b):
    #return -b * log(reliability)
    print({"code": 1, "msg": "", "sygzsj": -b * np.log(reliability)}, end = "")

#——————————————————————————————
#高斯分布求剩余工作时间
def find_gau(reliability, miu, sigma):
    reliability = 0.5
    worktime = 0.1
    low = 0
    high = worktime
    while (func_cdf_gau(high,miu, sigma) > reliability):
        high = high *2
    middle = (high + low) / 2
    count = 0
    while (abs(func_cdf_gau(middle, miu, sigma)-reliability) > 0.01 and count < 1000):
        if (func_cdf_gau(middle, miu, sigma) < reliability):
            high = middle
        else:
            low = middle
        middle = (high + low) / 2
        count += 1
        # print(middle)
    middle = round(middle, 4)
    #return middle
    print({"code": 1, "msg": "", "sygzsj": middle}, end = "")

#————————————————————————————————
#对数高斯分布求剩余工作时间
def find_loggau(reliability, miu, sigma):
    reliability = 0.5
    worktime = 0.1
    low = 0
    high = worktime
    while (func_cdf_loggau(high,miu, sigma) > reliability):
        high = high *2
    middle = (high + low) / 2
    count = 0
    while (abs(func_cdf_loggau(middle, miu, sigma)-reliability) > 0.01 and count < 1000):
        if (func_cdf_loggau(middle, miu, sigma) < reliability):
            high = middle
        else:
            low = middle
        middle = (high + low) / 2
        count += 1
        # print(middle)
    middle = round(middle, 4)
    # return middle
    print({"code": 1, "msg": "", "sygzsj": middle}, end = "")

#——————————————————————
#威布尔分布求剩余工作时间
def find_weib(reliability, m, gamma, t0):
    # return gamma + pow((-t0*log(reliability), 1/m))
    print({"code": 1, "msg": "", "sygzsj": gamma + pow(-t0*np.log(reliability), 1/m)}, end = "")

#——————————————————————————
#三次样条插值法求剩余工作时间
def find_cub(reliability, a,b,c):
    reliability = 0.5
    worktime = 0.1
    low = 0
    high = worktime
    while (func_cdf_cub(high,a,b,c) > reliability):
        high = high *2
    middle = (high + low) / 2
    count = 0
    while (abs(func_cdf_cub(middle, a,b,c)-reliability) > 0.01 and count < 1000):
        if (func_cdf_cub(middle, a,b,c) < reliability):
            high = middle
        else:
            low = middle
        middle = (high + low) / 2
        count += 1
        # print(middle)
    middle = round(middle, 4)
    # return middle
    print({"code": 1, "msg": "", "sygzsj": middle}, end = "")

# 部件级态势感知算法交付
if __name__ == "__main__":
    #run([1,2,3,4], type = 3)
    #print(func_weib(5, 3446685051.4268165, 0, 0.24999999999552736))
    a = {{a}} # a 是一个数组
    b = {{b}} # b 是一个数组
    c = {{c}}
    b1 = {{b1}}
    miu2 = {{miu2}}
    sigma2 = {{sigma2}}
    miu3 = {{miu3}}
    sigma3 = {{sigma3}}
    m = {{m}}
    gamma = {{gamma}}
    t0 = {{t0}}

    tp = {{tp}}
    reliability = {{reliability}}
    #run(x, type = tp, ct = ct)
    #print(func_weib(5, 3446685051.4268165, 0, 0.24999999999552736))

    '''
    type = 5 三次样条插值法
    '''
    if tp == 5: 
        find_cub(reliability, a,b,c)
    
    '''
    type = 4 威布尔分布
    '''
    if tp == 4: 
        find_weib(reliability, m, gamma, t0)


    '''
    type = 3 对数高斯分布
    '''
    if tp == 3:
        find_loggau(reliability, miu3, sigma3)

    '''
    type = 2 高斯分布
    '''
    if tp == 2:
        find_gau(reliability, miu2, sigma2)

    '''
    type = 1 指数分布
    '''
    if tp == 1:
        find_expon(reliability, b1)